<?php

namespace App\Listeners;

use App\Events\SendNoticeToSome;
use App\Models\Notification;

class SendMsgToSome
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param  SendNoticeToSome  $event
     * @return void
     */
    public function handle(SendNoticeToSome $event)
    {
        $data = $event->data;
        $type = $event->type;
        $msgType = $event->msgType;

        switch ($msgType) {
            case '1': //认购项目完成
                $title = "认购项目众筹成功";
                $content = "恭喜您所认购的项目已经成功，您获得的会员服务以及其他利益将会发放，感谢支持。";
                break;
            case '2': //认购项目失败
                $title = "认购项目众筹失败";
                if($data->is_trust == 1){
                    $content = "抱歉，您认购的项目失败，系统已将您的认购金额 投入到其他项目。（托管）";            
                }else{
                    $content = "抱歉，您认购的项目失败，您的认购金额将会返还给您，请注意查收!（未托管）";
                }
                break;
            case '3': //角色竞选
                $title = '';
                $content = '';
                break;
            default:
                break;
        }

        $ins = [
            'title' => $title,
            'content' => $content,
            'time' => date('Y-m-d H:i:s'),
        ];
        $ins_en = json_encode($ins);
        foreach ($data as $value) {
            $msg_data[] = [
                'user_id' => $value->user_id,
                'type' => $type,
                'data' => $ins_en,
                'create_id' => 0,
            ];
        }
        Notification::insert($msg_data);
    }
}
